﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing;

namespace ALDIOrganizer
{
    [Serializable]
    public partial class Cell
    {
        #region whereabouts
        // The Cell has to know where it is located, and who'pShift her "owner"
        private int _column;
        public int GetColumn
        {
            get
            {
                return _column;
            }
        }                
        
        
        internal void ActivateAndUpdate()
        {
            CellStyle = new CachedStyle();            
            Update(); 
        }
        
        public RowTag Tag;        
        
        public Guid GetIdentifier
        {
            get
            {
                return Tag.GuidValue;
            }
        }
        
        private Int64[] _statusID;
        
        /// <summary>
        /// DataFile id of _contract, _holiday or request associated with this Cell
        /// </summary>
        /// <remarks>
        /// chu
        /// </remarks>
        public Int64[] GetStatusID
        {
            get
            {
                return _statusID;
            }
        }

        #endregion
        
        public CellType MyCellType;
        

        private SpecialDayStatus GetMyStatus
        {
            get
            {
                if (GetParent == null)
                {
                    return (default(SpecialDayStatus));
                }
                if (GetParentMode == SheetType.Template | 
                    GetColumn == 0)
                {                    
                    return default(SpecialDayStatus);
                }
                return GetParentAsRota.DayStatuses[GetColumn - 1];                
            }
        }

        internal DateTime GetMyDate
        {
            get
            {
                if (GetParentMode == SheetType.Template)
                {
                    Program.LogError("cell:GetMyDate called even though mode is template", null);
                    // Debugger.Break();
                    return default(DateTime);
                }
                else
                {
                    return GetParentAsRota.Monday.AddDays(_column - 1).Date;
                }
            }
        }

        public bool HasAnyNotes
        {
            get
            {
                return !String.IsNullOrEmpty(Notes);
            }
        }
        
        public string Notes;

        public bool IsEditable
        {
            get
            {
                if (GetParentMode == SheetType.Rota)                    
                {
                    if (GetMyStatus.IsShut())
                    {
                        return false;
                    }
                }                
                switch (MyCellType)
                {
                    case CellType.Blank:
                    case CellType.Shift: return true;
                    default: return false;
                }
            }
        }

        private Cell(Sheet pParent, int pColumn)
        {
            _parent = pParent;
            _column = pColumn;
            Shifts = new ShiftTandem();
        }
                
        public Cell(Sheet pParent, int pColumn, RowTag pTag)
            : this(pParent, pColumn)
        {
            Tag = pTag;
        }        

        public override string ToString()
        {
            return Value;
        }

        public string ToStringWithNotes()
        {
            return String.Concat(
                ToString(),
                Environment.NewLine,
                "* ",
                Notes);
        }

        public string ToStringForMainForm()
        {
            var result = String.Empty;
            if (IsEditable)
            {
                if (GetParentMode == SheetType.Rota)
                {
                    result += GetMyEmployee.Employee.firstName
                        + ", ";
                }
                result += Time.ShortNameOfDay(GetColumn)
                    + " ";
                if (Shifts.Count > 0)
                {
                    result += Shifts.First.ToStringAbsolute();
                }
                if (Shifts.Count > 1)
                {
                    result += "; "
                        + Shifts.Second.ToStringAbsolute();
                }
            }
            if (MyCellType == CellType.Holiday)
            {
                result += Time.ShortNameOfDay(GetColumn)
                    + " ";

                result += new Holiday(SQLiteAccess.GetHolidays(GetStatusID[0])).type.GetTypeName();
            }
            return result;
        }

                /// <summary>
        /// Updates the Cell'pShift text contents (Value) and/or ComputeStyle (CellStyle) + its MyCellType (MyCellType)
        /// </summary>
        /// <param Name="pStyle">Is CellStyle to be updated?</param>
        /// <param Name="pTypeAndValue">Are MyCellType and Value to be updated?</param>
        public void Update(bool pStyle, bool pTypeAndValue)
        {
            if (pTypeAndValue)
            {
                UpdateValue();
            }
            if (pStyle)
            {
                if (CellStyle == null)
                {
                    CellStyle = new CachedStyle();
                }
                CellStyle = ComputeStyle();
            }
        }

        public void Update()
        {
            Update(true, true);
        }
        
    }
}
